
/**
*变压器 js
*/ 

var	transformerDS;
var transformerFormPanel;
function doTransformerDelete(id)
{
	Ext.MessageBox.confirm('确认删除', '确认删除该信息?', 
	function(btn) {
		if(btn == 'yes') // 选中了是按钮	
		{
			JTransformerWeb.deleteTransformerBO(id,function(data)
			{
				if(data)
				{ 
					Ext.Msg.alert('成功',"操作成功！"); 
					transformerDS.load({params:{start:0,limit:17,'<%=Constant.BTS_CODE%>':'<%=strbtscode%>'}});	 
					//transformerFormPanel.getForm().reset();			
				}else
				{
					Ext.Msg.alert("失败","操作失败!");
				}
			})
		}
	})	
}


Ext.onReady(function(){  
	Ext.QuickTips.init();
	Ext.form.Field.prototype.msgTarget = 'side';

	var transformerType = Ext.data.Record.create([								
							   {name: 'id', type: "string"},
							   {name: 'btscode', type: "string"},
							   {name: 'btsname', type: "string"},
							   {name: 'model', type: "string"}, 
							   {name: 'capacity', type: "string"}, 
							   {name: 'manufacturer', type: "string"},
							   {name: 'property', type: "string"}, 
							   {name: 'maintenance', type: 'string'},
							   {name: 'memo',  type: 'string'},							   
							   {name: 'tranPowerType', type: "string"},
							   {name: 'tranMaintain', type: "string"},
							   {name: 'tranHighLen', type: "string"},							   
							   {name: 'refreshtime',  type: 'date'},							   
							   {name: 'tranLowLen',  type: 'string'},
							   {name: 'tranRValue',  type: 'string'},
							   {name: 'tranStartTime',  type: 'string'}
						 ]);
	var transformerCM = new Ext.grid.ColumnModel(
			[
				new Ext.grid.CheckboxSelectionModel({singleSelect: true}), 	            
				{header: "流水号", sortable: true, dataIndex: 'id',align:'left'},
				{header: "基站编号", sortable: true, dataIndex: 'btscode',align:'left'},
				{header: "基站名称",  sortable: true, dataIndex: 'btsname',align:'left'},	
				{header: "型号",  sortable: true, dataIndex: 'model',align:'left'},	
				{header: "厂家",  sortable: true, dataIndex: 'manufacturer',align:'left'},
				{header: "变压器产权",  sortable: true, dataIndex: 'property',align:'left'}, 				     	 
				{header: "删除", dataIndex: 'id',renderer: showDelete}					
			]
		);				 
	
	function showDelete(id)
	{
		return "<input type='button' value='删除' class='x-btn button' onclick='doTransformerDelete(\"" + id  +   "\")'>";  
	}
		
	transformerDS = new Ext.data.Store({
				proxy: new Ext.data.DWRProxy(JTransformerWeb.getTransformerList, true),	
				reader: new Ext.data.ListRangeReader({
					totalProperty: 'totalSize',
					id: 'id'
				}, transformerType),
				sortInfo:{field:'id', direction:'ASC'}// 排序信息
			});

	
	var towerMountedGridDiv = Ext.get("mainTabPanelDIV");
	var transformerGrid = new Ext.grid.GridPanel({			 	
			ds: transformerDS,
			cm: transformerCM, 
			sm: new Ext.grid.RowSelectionModel({
                    singleSelect: true,
                    listeners: {
                        rowselect: function(sm, row, rec) {
                            transformerFormPanel.getForm().loadRecord(rec);
                        }
                    }
                }),
			autoWidth : true,  
			autoScroll : true,  
			autoHeight　: false,
			region : 'center',
			height: 400,
			minSize: 200,		
			frame:true,
			collapsible:false, 
			enableDragDrop:false, 		
			bbar: new Ext.PagingToolbar({
					pageSize: 17,
					store: transformerDS,
					displayInfo: true,
					displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
					emptyMsg: "没有记录"
				})
		});
		
	transformerDS.on('load', function() {
		transformerGrid.getSelectionModel().selectFirstRow(); 
	});		

 	var tranPowerTypeStore  = new Ext.data.Store({  
		        proxy:new Ext.data.DWRProxy(JCommWeb.getDirctoryByType,{}),  
		        reader:new Ext.data.DWRArrayReader(
						{},
						new Ext.data.Record.create([  
							{name: 'text', mapping:1},       
							{name: 'value',mapping:0}                   
						  ])
						)  
		    }); 		
		
	
	var tranPowerTypeCombo = new Ext.form.ComboBox({	   
		fieldLabel: '电源类型',
		name: 'tranPowerType',	
		width:150,
		valueField :"value",
	    displayField: "text",
	    mode: 'local',
		selectOnFocus:true,                    
		forceSelection: true, 
		hiddenName:'tranPowerType',
		triggerAction: 'all', 
		editable:false, 
		store: tranPowerTypeStore
	});		
			
	tranPowerTypeStore.on('beforeload', function() {
			  tranPowerTypeStore.baseParams = {
					DIRECTORY_TYPE:'24'   
			  };
		});	
	tranPowerTypeStore.load({params:{DIRECTORY_TYPE:'24'}});
	
			
	
	transformerFormPanel = new Ext.form.FormPanel({
		region     : 'south', 
		bodyStyle  : 'padding: 10px; background-color: #DFE8F6',
		labelWidth : 130,  
		height: 190,
		minSize: 200,
		maxSize: 200,
		frame:true,
		buttonAlign:'right',
		layout:'column', 
		items:[
			 {
				columnWidth:.33,
				layout: 'form',  
				labelAlign:'right',	
				labelWidth:100,	 
				items: [
					{
						xtype:'hidden',
						fieldLabel: '流水号',
						width:150,
						name: 'id'
					}, {
						xtype:'textfield',
						readOnly: true,
						fieldLabel: '基站编号',
						width:150,
						name: 'btscode'
					}, {
						xtype:'textfield',
						fieldLabel: '基站名称',
						width:150,
						readOnly: true,
						name: 'btsname'
					}, {
						xtype:'textfield',
						fieldLabel: '容量',
						width:150,
						name: 'capacity'
					}, 
					tranPowerTypeCombo
					, {
						xtype:'textfield',
						fieldLabel: '变压器产权',
						width:150,
						name: 'property'
					}
					]
			 },
			 {
				columnWidth:.33,
				layout: 'form',  
				labelAlign:'right',	
				labelWidth:110,	 
				items: [
					{
						xtype:'textfield',
						fieldLabel: '型号',
						width:150,
						name: 'model'
					},
					{
						xtype:'textfield',
						fieldLabel: '厂家',
						width:150,
						name: 'manufacturer'
					}
					, {
						xtype:'textfield',
						fieldLabel: '高压端距离',
						width:150, 
						value:-1,
						name: 'tranHighLen'
					}
					, {
						xtype:'textfield',
						fieldLabel: '低压端距离',
						width:150,
						name: 'tranLowLen'
					}, {
						xtype:'textfield',
						fieldLabel: '地阻值',
						width:150,
						name: 'tranRValue'
					}]
			 },
			 {
				columnWidth:.33,
				layout: 'form',  
				labelAlign:'right',	
				labelWidth:110,	 
				items: [
					{
						xtype:'textfield',
						fieldLabel: '维修及更换记录',
						width:150, 
						name: 'maintenance'
					}
					,{
						xtype:'textfield',
						fieldLabel: '变压器维修及保养记录',
						width:150,
						name: 'tranMaintain'
					}					
					,{
						xtype:'datefield',
						fieldLabel: '启用时间',
						format : "Y-m-d",
						allowBlank:false,
						width:150,
						name: 'tranStartTime'
					}					
					,{
						xtype:'textarea',
						fieldLabel: '备注',
						width:150,
						name: 'memo'
					}]
			 }
			],
		buttons :[
			{
				text : ' 保存',
				handler : saveTransformer
			},{
				text : ' 新建/取消',
				handler : function(){
					transformerFormPanel.getForm().reset(); 
					transformerFormPanel.getForm().loadRecord(recordForAll);	
				}
			}
			]	
	});



	//Simple 'border layout' panel to house both grids
	
	var displayPanel = new Ext.Panel({
		width    : towerMountedGridDiv.getComputedWidth(),
		height   : 500,
		layout   : 'border',
		renderTo : 'TransformerTab',
		items    : [
			transformerGrid,
			transformerFormPanel
		]
	});
	
	
	function saveTransformer()
	{
		if (!transformerFormPanel.getForm().isValid())
			return;	
			
		var form = transformerFormPanel.getForm().getValues(false);	
		TransformerBO = {};
		TransformerBO.id = form.id;
		TransformerBO.btscode = form.btscode;
		TransformerBO.btsname= form.btsname;
		TransformerBO.model = form.model;
		TransformerBO.capacity = form.capacity;		
		TransformerBO.manufacturer = form.manufacturer;
		TransformerBO.property = form.property;
		TransformerBO.maintenance = form.maintenance;
		TransformerBO.memo = form.memo;
		TransformerBO.tranPowerType = form.tranPowerType;
		TransformerBO.tranMaintain = form.tranMaintain;
		TransformerBO.tranHighLen = form.tranHighLen;
		TransformerBO.tranLowLen = form.tranLowLen;
		TransformerBO.tranRValue = form.tranRValue;
		TransformerBO.tranStartTime = form.tranStartTime; 		
	   			
		JTransformerWeb.saveTransformerBO(TransformerBO,function(data)
		{
			if(data)
			{ 
				Ext.Msg.alert('成功',"操作成功！"); 
				transformerDS.load({params:{start:0,limit:17,'<%=Constant.BTS_CODE%>':'<%=strbtscode%>'}});	
				transformerFormPanel.getForm().reset(); 
				var dataTowerMounte = {
										'btscode':"<%=strbtscode%>",
										'btsname':"<%=strbtsname%>"
									};	 		
				var recordTowerMounte = new Ext.data.Record(dataTowerMounte); 
				transformerFormPanel.getForm().loadRecord(recordTowerMounte);	 
			}else
			{
				Ext.Msg.alert("失败","操作失败!");
			}
		})
			
	}
	
	loadDate(transformerDS,transformerFormPanel);
	
})

